dojo.require("selfpass.module.Account");
dojo.require("selfpass.module.User");

function createCatalogMenu(){
	if(dijit.byId("_catalogMenuButton")){
		return false;
	}else{
		var _createAccount = new dijit.MenuItem({
			label: "Create Account", 
			onClick: createAccountButtonEvt
			});
		var _creatCatalog = new dijit.MenuItem({
			label: "Create Catalog", 
			onClick: createCatalogButtonEvt
			});
		var _modifyCatalog = new dijit.MenuItem({
			label: "Modify Catalog", 
			onClick: modifyCatalogButtonEvt
			});
		var _deleteCatalog = new dijit.MenuItem({
			id: "_deleteCatalog",
			label: "Delete Catalog", 
			onClick:deleteCatalogButtonEvt
			});
		var _catalogMenu = new dijit.Menu({
			id:"_catalogMenu"
			});
		_catalogMenu.addChild(_createAccount);
		_catalogMenu.addChild(new dijit.MenuSeparator);
		_catalogMenu.addChild(_creatCatalog);
		_catalogMenu.addChild(_modifyCatalog);
		_catalogMenu.addChild(_deleteCatalog);
		var dropButton = new 	dijit.form.DropDownButton({
			id:"_catalogMenuButton",
			label:"Catalog Operation",
			dropDown: _catalogMenu
		});
		dojo.byId("catalogToolBar").appendChild(dropButton.domNode);
	}
}

function createAccountMenu(){
	if(dijit.byId("_accountMenuButton")){
		return false;
	}else{
		var _createAccount = new dijit.MenuItem({
			label: "Create Account", 
			onClick: createAccountButtonEvt
			});
		var _modifyAccount = new dijit.MenuItem({
			id: "_modifyAccount",
			label: "Modify Account", 
			onClick: modifyAccountButtonEvt
			});
		var _deleteAccount = new dijit.MenuItem({
			id: "_deleteAccount",
			label: "Delete Account", 
			onClick: deleteAccountButtonEvt
			});
	    var _accountMenu = new dijit.Menu({
			id:"_accountMenu"
			});
		_accountMenu.addChild(_createAccount);
		_accountMenu.addChild(_modifyAccount);
		_accountMenu.addChild(_deleteAccount);
		var dropButton = new 	dijit.form.DropDownButton({
			id:"_accountMenuButton",
			label:"Account Operation",
			dropDown: _accountMenu
		});
		dojo.byId("accountToolBar").appendChild(dropButton.domNode);
	}
}
//addOnLoad
//set buttons into toolBar
function createTopMenu(){
	var _userSetting = new dijit.form.Button({
		label: "User Setting"
		//onClick: userSetting
		});
	var _themeSetting = new dijit.form.Button({
		label: "Theme Setting"
		//onClick: themeSetting
		});
	var _logout = new dijit.form.Button({
		label: "Logout", 
		onClick:doLogout
		});
	dojo.byId("toolBar").appendChild(_userSetting.domNode);
	dojo.byId("toolBar").appendChild(_themeSetting.domNode);
	dojo.byId("toolBar").appendChild(_logout.domNode);
}

//addOnLoad
//load catalog tree
function showTree(){			
	dojo.xhrPost({
			url: "/service/showCatalogTree",
			handleAs: "json",
			handle: function(data,args){
				//destroy old tree if there is
				handleWidgetDestroy(dojo.query(".dijitAccordionBody,#treeSpace")[0]);				
				setTimeout(function(){
					//create tree div
					var treeDiv = document.createElement("div");
					treeDiv.id="tree";
					dojo.query(".dijitAccordionBody,#treeSpace")[0].appendChild(treeDiv);
					 //create tree
					var treeData = data;	
					var store = new dojo.data.ItemFileReadStore({data:treeData});
					var _tree = new dijit.Tree({
						  "id":"tree",
						  "store":store,
						  "query":{type:"root"},
						  "labelAttr":"name",
						  "onClick" : clickCatalogNode,
						  "getIconClass" : 
							  function(item){
							    if(item && item.type=="root"){
									return "dijitTreeIcon dijitFolderOpened";
								}else{
									return "dijitTreeIcon dijitFolderOpened";
								}
						      }
						  }
						  ,dojo.byId("tree")
						  );
					
					//init currentRootCatalog
					store.fetch({
						query:{type:"root"},
						onComplete:function(items,req){
							currentRootCatalogKey = items[0].key;
							currentRootCatalogNode = dijit.byNode(dojo.query(".dijitTreeNode,#tree")[1]);
						}
					});
					//init delete disabled
					 dijit.byId("_deleteCatalog").setDisabled(true);
				},200);
			}
	});			
}


//After event
//show accountList
function showAccountList(catalogId){
	dojo.xhrPost({
				url: "/service/showAccountList",
				handleAs: "json",
				handle: function(data,args){
					var model = new dojox.grid.data.Objects(null, data.items);
					model.query={type:"account"};
					var layout = {
						noscroll: true,
						rows: [[
							{ name: "name", field: "name",width:"20%"},
							{ name: "id" , field:"accountId",width:"20%"},
							{ name: 'password', field: 'password',formatter:decrypt,width:"20%"},
							{ name: "url" , field:"url",width:"40%"}
						]]
					};
					handleWidgetDestroy(dojo.byId("accountList"));
					var grid = new dojox.Grid({
						  "id": "accountListGrid",
						  "model": model,
						  "structure": [layout],
						  "onRowDblClick" : dblClickAccountRow,
						  "onRowClick" : clickAccountRow,
						  "autoHeight":true
					});
					dojo.byId("accountList").appendChild(grid.domNode);
					grid.render();

					 //init modify and delete disabled
					 dijit.byId("_modifyAccount").setDisabled(true);
					 dijit.byId("_deleteAccount").setDisabled(true);
				},
				content:{"catalogId":	catalogId}
		});
}

//After event
//show accountDetails
function showAccountDetails(account,isNew){
	clearDiv("detailsSpace");
	var form = 	createAccountForm();
	dijit.byId("detailsSpace").addChild(createDefaultDetailsSpace(form));
	if(isNew){
			setInner("detailspaceTitle","New Account");
			dijit.byId("accountSubmit").attr("label"," New It ");
			dijit.byId("accountSubmit").attr("onClick", newAccount);
	}else{
			setInner("detailspaceTitle","Edit Account");
			dijit.byId("accountSubmit").attr("label"," Modify It ");
			dijit.byId("accountSubmit").attr("onClick", modifyAccount);
	}
	dojo.byId("account_catalog").value =account.catalog.name;
	dojo.byId("account_catalog_key").value =account.catalog.id;
	dojo.byId("account_name").value =account.name;
	dojo.byId("account_key").value =account.id;
	dojo.byId("account_id").value =account.accountId;
	dojo.byId("account_pwd").value =account.pwd;
	dojo.byId("account_url").value =account.url;
	dojo.byId("account_des").value =account.description;
}

//After event
//show accountDetails
function showCatalogDetails(catalog,isNew){
	clearDiv("detailsSpace");
	var form = 	createCatalogForm();
	dijit.byId("detailsSpace").addChild(createDefaultDetailsSpace(form));
	if(isNew){
			setInner("detailspaceTitle","New Catalog");
			dijit.byId("catalogSubmit").attr("label"," New It ");
			dijit.byId("catalogSubmit").attr("onClick", newCatalog);

			dojo.byId("parent_catalog").value =catalog.name;
			dojo.byId("parent_catalog_key").value =catalog.id;
	}else{
			setInner("detailspaceTitle","Edit Catalog");
			dijit.byId("catalogSubmit").attr("label"," Modify It ");
			dijit.byId("catalogSubmit").attr("onClick", modifyCatalog);

			dojo.byId("catalog_key").value =catalog.id;
			dojo.byId("catalog_name").value =catalog.name;
			dojo.byId("catalog_des").value =catalog.description;
			dojo.byId("parent_catalog").value =catalog.parent.name;
			dojo.byId("parent_catalog_key").value =catalog.parent.id;
	}
}


//After dispatch
//show mainPage
function showMain(nickName){
		document.title="Self Pass-My Home";
		clearDiv("whole");
		var _wholeDiv = createInternalSpace();
		dojo.byId("whole").appendChild(_wholeDiv.domNode);
		_wholeDiv.startup();
		createTopMenu();
		setInner("welcome","welcome, Dear "+user.nickName,200);
		showTree();
		createCatalogMenu();
		createAccountMenu();
}

//After dispatch
//show initPage
function showInit(){
		document.title="Self Pass-User Init";
		clearDiv("whole");
		var _title = createExteriorTitle("User Init");
		var _summary = createSummaryMsg("The first time login,need init user first");
		var _form = createInitForm();
		var _wholeDiv = createExteriorSpace(titleDiv=_title, summaryDiv=_summary, form=_form);
		dojo.byId("whole").appendChild(_wholeDiv.domNode);	
		_wholeDiv.startup();
}

//After dispatch
//show LoginPage
function showLogin(){
		document.title="Self Pass-User Login";
		clearDiv("whole");
		var _title = createExteriorTitle("User Login");
		var _summary = createSummaryMsg("Please input your name and password.");
		var _form = createLoginForm();
		var _wholeDiv = createExteriorSpace(titleDiv=_title, summaryDiv=_summary, form=_form);
		dojo.byId("whole").appendChild(_wholeDiv.domNode);	
		_wholeDiv.startup();
}